/*
 * strselect.c
 *
 *  Created on: Apr 4, 2012
 *      Author: root
 */

//#define look

#ifdef look

#include	"unp.h"

#define IDSIZE 128

char* space_remover(char* dst, char* src, size_t srclen);
char* space_remover2(char* src, size_t srclen);
void debug(char *cont, int debug);
void client_interface(char* src);
void console(int command_set, char* id);

char* LOG_STRING = "230 User logged in";
char* ACK_STRING = "200 OK";
char* MSG_STRING = "299 Message delivered";
char* QUIT_STRING = "221 Service closing";
char* LOG_ERR_STRING = "530 Not logged in";
char* SYNTAX_ERR_STRING = "500 Syntax error, command unrecognized";

int debug_mode;


void str_cli(FILE *fp, int sockfd) {
	debug_mode = 0;
	int maxfdp1, stdineof;
	fd_set rset;
	char send_buf[MAXLINE];
	char rcv_buf[MAXLINE];

	char msg[MAXLINE];
	char parsing[MAXLINE];
	char* fromid_p;
	char* toid_p;
	char toid[IDSIZE];
	char id[IDSIZE];
	ssize_t n;
	char* p1;
	char* p2;

	int log_set = 0;
	int command_set = 1;

	char* p;

	stdineof = 0;
	FD_ZERO(&rset);
	for (;;) {
		memset(send_buf, 0x00, MAXLINE);
		memset(parsing, 0x00, MAXLINE);
		memset(msg, 0x00, MAXLINE);
		memset(rcv_buf, 0x00, MAXLINE);

		if (stdineof == 0)
			FD_SET(fileno(fp), &rset);
		FD_SET(sockfd, &rset);
		maxfdp1 = max(fileno(fp), sockfd) + 1;

		console(command_set, id);
		Select(maxfdp1, &rset, NULL, NULL, NULL);

		if (FD_ISSET(sockfd, &rset)) {
			memset(send_buf, 0x00, MAXLINE);
			memset(rcv_buf, 0x00, MAXLINE);

			if ((n = my_readcrlf(sockfd, rcv_buf, MAXLINE)) == 0) {

				if (stdineof == 1)
					return;
				else
					err_quit("str_cli: server terminated prematurely22222\n");

			} else {
				debug(rcv_buf, debug_mode);
				printf("\n");
				client_interface(rcv_buf);
				//Write(fileno(stdout), rcv_buf, n);
			}
		}
		//console(command_set, id);

		if (FD_ISSET(fileno(fp), &rset)) {

			memset(rcv_buf, 0x00, MAXLINE);

			if ((n = my_readline(fileno(fp), rcv_buf, MAXLINE)) == 0) {
				stdineof = 1;
				Shutdown(sockfd, SHUT_WR);
				FD_CLR(fileno(fp), &rset);
			} else {

				memset(parsing, 0x00, MAXLINE);
				memset(msg, 0x00, MAXLINE);

				p1 = space_remover(parsing, rcv_buf, strlen(rcv_buf));


				if (command_set == 1) {

					if (!strcmp(parsing, "user")) {

						p1 = space_remover2(p1, strlen(p1)); // p1 = id
						sprintf(id, "%s", p1);
						sprintf(msg, "USER %s\r\n\r\n", id);
						debug(msg, debug_mode);
						write(sockfd, msg, strlen(msg));
						//sprintf(msg, "%s\r\n", id);
						log_set = 1;
					} else if (!strcmp(parsing, "quit")) {
						sprintf(msg, "QUIT\r\n\r\n");
						debug(msg, debug_mode);
						write(sockfd, msg, strlen(msg));

						stdineof = 1;
						Shutdown(sockfd, SHUT_WR); // send FIN
						FD_CLR(fileno(fp), &rset);
					} else if (!strcmp(parsing, "help")) {

						printf("Usage :\n");
						printf("who 	-- who are logged in\n");
						printf("talk  [<[peer>|all]	-- start talk session with <peer> or with all the loggined users\n");
						printf("quit 	-- quit session\n");
						printf("user  <user ID>	-- loggin with <user ID>}\n");
						printf("debug 	-- debug mode on or off (toggle)\n");
						printf("help 	-- show usage\n");
						printf(". 	-- end of talk session\n");

					} else if (!strcmp(parsing, "who")) {
						sprintf(msg, "WHO\r\n\r\n");
						debug(msg, debug_mode);
						write(sockfd, msg, strlen(msg));
					} else if (!strcmp(parsing, "debug")) {
						printf("DEBUG\n");
						if (debug_mode == 0)
							debug_mode = 1;
						else
							debug_mode = 0;
					} else if (!strcmp(parsing, "talk")) {
						p1 = space_remover(toid, p1, strlen(p1)); // toid
						//id_set = 1;
						command_set = 0;
					} else {
						printf("typing error\n");
					}
				} else if (command_set == 0) {
					if (!strcmp(parsing, ".")) {
						printf("END of TALK\n");
						command_set = 1;
					} else {
						sprintf(msg, "FROM %s TO %s\r\n%s\r\n\r\n", id, toid,
								rcv_buf);
						debug(msg, debug_mode);
						write(sockfd, msg, strlen(msg));
					}
				}
			}
		}
	}
}


void client_interface(char* src) {

	char parsing[MAXLINE];

	memset(parsing, 0x00, MAXLINE);

	char* p1;
	char* p2;

	int log_set = 0;

	char fromid[IDSIZE];
	char toid[IDSIZE];
	char message[MAXLINE];
	char buf[MAXLINE];
	char msg[MAXLINE];
	memset(msg, 0x00, MAXLINE);

	p1 = space_remover(parsing, src, strlen(src));


	if (!strcmp(parsing, "230")) {
		log_set = 1;
		printf("SUCCESS LOG IN\n");
	} else if (!strcmp(parsing, "221")) {
		printf("Disconnect.. \n");
	} else if (!strcmp(parsing, "530")) {
		printf("You are not log in\n");
	} else if (!strcmp(parsing, "500")) {
		printf("Syntax error !!!!!!\n");
	} else if (!strcmp(parsing, "200")) {
			//printf("complete send message\n");

	} else if (!strcmp(parsing, "210")) {
			p1 = space_remover(buf, p1, strlen(p1)); // p1 = ID list
			p1 = space_remover2(p1, strlen(p1));
			printf("ID LIST : %s\n", p1);

	} else if (!strcmp(parsing, "299")) {
		strtok(p1, "\r\n");
		p1 = strtok(NULL, "\r\n"); // FROM ID1 TO ID2
		p2 = strtok(NULL, "\r\n"); // MESSAGE

		p1 = space_remover(buf, p1, strlen(p1)); // FROM
		p1 = space_remover(fromid, p1, strlen(p1)); //ID1
		//sprintf(fromid,"%s",p1);
		sprintf(message, "%s", p2);

		printf("\n%s ) %s\n", fromid, message);

	} else {
		printf("Server error!!\n");
	}
}


char* space_remover(char* dst, char* src, size_t srclen) {

	size_t n;
	char c;
	char* start = src;
	char* end = src;
	int i = 0;

	for (n = 1; n <= srclen; n++) {
		if ((*src == ' ') || (*src == '\n') || (*src == '\r')) {
			src++;
			continue;
		} else {
			while ((*src != ' ') && (*src != '\n') && (*src != '\r')
					&& (*src != 0)) {
				*dst++ = *src;
				src++;
				end = src;
				n++;
			}
			*dst = 0;
			break;
		}
	}
	return end;
}

char* space_remover2(char* src, size_t srclen) {

	size_t n;
	char c;
	char* start = src;

	for (n = 1; n <= srclen; n++) {
		if ((*src == ' ') || (*src == '\n') || (*src == '\r')) {
			src++;
			continue;
		} else {
			return src;
		}
	}
	return start;
}

void debug(char *cont, int debug) {
	if (debug == 1) {
		write(fileno(stderr), cont, strlen(cont));
		printf("\n");
	}
}


void console(int command_set, char* id) {
	if (command_set == 1)
		printf("> ");
	else if (command_set == 0)
		printf("%s : ", id);

	fflush(stdout);
}


#endif
